<?php

   $conn= mysql_connect("localhost","root","root") or die("Could not connect to MySQL");
   mysql_select_db("postit");
     
   $q=mysql_query("SELECT distinct(waypoint.id),latitude,longitude FROM postit.waypoint, postit.postit 
			WHERE postit.waypoint=waypoint.id AND postit.cathegory='".$_REQUEST['cathegory']."';");
   while($e=mysql_fetch_assoc($q))
        if (distance($_REQUEST['myLatitude'],$_REQUEST['myLongitude'],$e['latitude'],$e['longitude'])<$_REQUEST['radius'])
           $output[]=$e;

   print(json_encode($output));    
   mysql_close();


  function distance($lat1, $lon1, $lat2, $lon2) { 
     $theta = $lon1 - $lon2; 
     $dist = sin(deg2rad($lat1)) * sin(deg2rad($lat2)) +  cos(deg2rad($lat1)) * cos(deg2rad($lat2)) * cos(deg2rad($theta)); 
     $dist = acos($dist); 
     $dist = rad2deg($dist); 
     $miles = $dist * 60 * 1.1515;
     return ($miles * 1.609344);
  }

?>
